$(document).ready(function(){
	var token=localStorage.getItem("token");
	var addUser=localStorage.getItem("username");
	$("#addUser").val(addUser);
	//字典取值
	$.ajax({
		type:"post",
		url:server_url+"/rest/dictionary/getDicNameValue.do?token="+token,
		data:{
			typeName:'性别'
		},
		success:function(data){
			if(data.retCode=="0000"){
				if(data.data.length>0){
					var html='<option value="">---请选择---</option>';
					$(html).appendTo("#sex");
					for(var i in data.data){
						html='<option value='+data.data[i].dicValue+'>'+data.data[i].dicName+'</option>';
						$(html).appendTo("#sex");
					}
				}
			}else {
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
			
		},
	});
	 
	$.ajax({
		type:"post",
		url:server_url+"/rest/dictionary/getDicNameValue.do?token="+token,
		data:{
			typeName:'职位状态'
		},
		success:function(data){
			if(data.retCode=="0000"){
				if(data.data.length>0){
					for(var i in data.data){
						var html='<option value='+data.data[i].dicValue+'>'+data.data[i].dicName+'</option>';
						$(html).appendTo("#employStatus");
					}
					$("#employStatus option").each(function(i,v){
						if($(v).text()=='在职'){
							$(v).attr("selected","selected");
						}
					});
				}
			}else {
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
		},
	});
	//初始化时间控件，出生日期小于入职日期
	$("#birthday").datetimepicker({
		lang:'ch',
		timepicker:false,
		format:'Y-m-d',
		formatDate:'Y-m-d',
		onShow:function(){
			this.setOptions({
				maxDate:$("#employTime").val()?$("#employTime").val():0
			});
		}
	});
	$("#employTime").datetimepicker({
		lang:'ch',
		timepicker:false,
		format:'Y-m-d',
		formatDate:'Y-m-d',
		maxDate:0,
		onShow:function(){
			this.setOptions({
				minDate:$("#birthday").val()?$("#birthday").val():false
			});
		}
	});
	
	$(".z_goback").click(function(){
		var url="staffList.do?ran="+Math.random();
		tl_config.loadToWorkArea(url);
	});
	
	$(".pccErrMsg").css("display","none");

	$("#form_staff").validate({
		rules:{
			staffNo:{
				required:true,
				remote: { 
	                   url: server_url+'rest/isExistStaffNo.do?token='+token+'&ran='+Math.random(), 
	                   type: "get",
	                   dataType: "json", 
	                   data: {			                	     
	                	   staffNo: function() { return $("#staffNo").val();}
                       } 
				}
				},
			loginName : {
				required : true,
				remote: { 
	                   url: server_url+'rest/isExistLoginName.do?token='+token+'&ran='+Math.random(), 
	                   type: "get",
	                   dataType: "json", 
	                   data: {			                	     
	                	   loginName: function() { return $("#loginName").val();}
                    } 
				}

			},
			staffName : {
				required : true

			},
			mobile : {
				required : true,
				isMobile: true,
				remote: { 
	                   url: server_url+'rest/isExistMobile.do?token='+token+'&ran='+Math.random(), 
	                   type: "get",
	                   dataType: "json", 
	                   data: {			                	     
	                	   mobile: function() { return $("#mobile").val();}
                    } 
				}

			},
			email : {
				required : true,
				email :true,
				remote: { 
	                   url: server_url+'rest/isExistEmail.do?token='+token+'&ran='+Math.random(), 
	                   type: "get",
	                   dataType: "json", 
	                   data: {			                	     
	                	   email: function() { return $("#email").val();}
                    } 
				}

			},
			sex :{
				required : true
			}
			
		},
		messages:{
			staffNo:{
				required:"该值不能为空！",
				remote:"员工编号已存在"
					},
			loginName:{
				required:"该值不能为空！",
				remote:"用户名已存在"
					},
			staffName : {
				required:"该值不能为空！"
			},
			mobile:{
				required:"该值不能为空！",
				isMobile:"手机号格式错误！",
				remote:"手机号已存在"
					},
			email:{
				required:"该值不能为空！",
				email:"邮箱格式错误！",
				remote:"邮箱已存在"
			},
			sex:{
				required:"该值不能为空！"
			}
		},
		submitHandler:function(){
			commit();
		},
		errorPlacement: function(error, element) {
		    element.next().next().html(error);
		}
		
	});
});

function commit(){
	if(checkArea()){
		$.ajax({
			type:"post",
			url:server_url+'rest/addStaff.do?token='+token+'&ran='+Math.random(),
			data:$("#form_staff").serialize(),
			success:function(data){
				if(data.retCode=="0000"){
					id=data.data;
					var url='assignPost.do?id='+id+"&ran="+Math.random();
					tl_config.loadToWorkArea(url);
				} else {
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:data.retMsg
						},
						autoClose : 2000
					});
				}
			}
		});
	}
}
function checkArea(){
	var selProvince = document.getElementById("provinceId");
	var selCity = document.getElementById("cityId");
	var selCounty = document.getElementById("countyId");
	var provinceIndex=selProvince.selectedIndex;
	var cityIndex=selCity.selectedIndex;
	var countyIndex=selCounty.selectedIndex;
	if(provinceIndex==0||cityIndex==0||countyIndex==0){
		$(".pccErrMsg").css("display","block");
		return false;
	}else{
		$(".pccErrMsg").css("display","none");
		return true;
	}
}

function submitCommit(){
	$("#form_staff").submit();
}
